package com.zh.kotlin_mvvm.ui

import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.ClipData
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.core.content.FileProvider
import com.zh.common.base.BaseActivity
import com.zh.config.fragment.PictureFragment
import com.zh.kotlin_mvvm.databinding.ActivityShareBinding
import java.io.File


class ShareActivity : BaseActivity<ActivityShareBinding>() {

    private lateinit var pictureFragment: PictureFragment

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP_MR1)
    override fun initView(savedInstanceState: Bundle?) {

        pictureFragment = supportFragmentManager.findFragmentByTag("pictureF") as PictureFragment

        viewBinding.btnShare.setOnClickListener {
            if (pictureFragment.getPictureList().isNotEmpty()) {
                shareText(pictureFragment.getPictureList()[0].realPath)
            } else {
                shareText("")
            }
        }
        viewBinding.btnWx.setOnClickListener {
            shareToWx()
        }
    }

    // 分享类型: text/plain、application/*、image/*、video/*、audio/*
    @RequiresApi(Build.VERSION_CODES.LOLLIPOP_MR1)
    private fun shareText(imgPath: String) {
        val sendIntent = Intent()
        sendIntent.action = Intent.ACTION_SEND
        sendIntent.putExtra(Intent.EXTRA_TEXT, "我要分享的文本\nhttps://www.baidu.com")

        // Android 10 开始，可以通过 Intent.EXTRA_TITLE 添加描述信息，ClipData 添加缩略图
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && imgPath.isNotEmpty()) {
            val imageUri = FileProvider.getUriForFile(
                this, "$packageName.fileprovider", File(imgPath)
            )
            sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            sendIntent.putExtra(Intent.EXTRA_TITLE, "我是图片标题")
            sendIntent.putExtra(Intent.EXTRA_STREAM, imageUri)
            sendIntent.clipData = ClipData.newUri(contentResolver, "我是缩略图", imageUri)
            sendIntent.type = "image/*"
        } else {
            sendIntent.type = "text/plain"
        }
        val pi = PendingIntent.getBroadcast(
            this, 10000, intent, PendingIntent.FLAG_UPDATE_CURRENT
        )
        val shareIntent = Intent.createChooser(sendIntent, "分享到", pi.intentSender)
        startActivity(shareIntent)
    }

    private fun shareToWx() {
        val wechatIntent = Intent(Intent.ACTION_SEND)
        wechatIntent.setPackage("com.tencent.mm")
        wechatIntent.type = "text/plain"
        wechatIntent.putExtra(Intent.EXTRA_TEXT, "分享到微信的内容")
        startActivity(wechatIntent)
    }

    class IntentSenderBroadcastReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            val extra = intent.getParcelableExtra<ComponentName>(Intent.EXTRA_CHOSEN_COMPONENT)
            Toast.makeText(context, "点击了 " + extra!!.packageName, Toast.LENGTH_SHORT).show()
        }
    }
}